package com.lenovo.smarttraffic.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.util.FormatUtils;
import com.lenovo.smarttraffic.util.ToastUtils;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends BaseActivity {

    private EditText mEditAccount;
    private EditText mEditEmail;
    private EditText mEditPwd;
    private EditText mEditPwdCheck;
    private EditText mEditPhone;
    private Button mRegisterButton;

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();


        //用户名不能少于4位数
        mEditAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                char[] chars = s.toString().toCharArray();
                if (chars.length < 4) {
                    mEditAccount.setError("当前账号名小于4位");
                }
                String regex = "[0-9a-zA-Z]";
                for (char aChar : chars) {
                    if (!(String.valueOf(aChar)).matches(regex)) {
                        mEditAccount.setError("账号只能包括大小写字母和数字");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //验证邮箱
        mEditEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!FormatUtils.isEmail(s.toString())) {
                    mEditEmail.setError("请输入正确的邮箱");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //用户密码不能少于6位数字
        mEditPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                char[] chars = s.toString().toCharArray();
                if (chars.length < 6) {
                    mEditPwd.setError("当前密码小于6位");
                }
                String regex = "[0-9a-zA-Z]";
                for (char aChar : chars) {
                    if (!String.valueOf(aChar).matches(regex)) {
                        mEditPwd.setError("密码只能包括大小写字母和数字");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        //用户密码二次确认
        mEditPwdCheck.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals(mEditPwd.getText().toString())) {
                    mEditPwdCheck.setError("两次输入密码不一致");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        //用户手机号验证
        mEditPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\\d{8}$";
                Pattern p = Pattern.compile(regExp);
                Matcher matcher = p.matcher(s);
                char[] chars = s.toString().toCharArray();
                //进行输入判断
                if (chars.length > 11) {
                    mEditPhone.setError("当前手机号大于11位");
                    mEditPhone.setText(s.toString().substring(0, 11));
                    mEditPhone.setSelection(0, 11);
                } else {
                    if (!matcher.matches()) {
                        mEditPhone.setError("请输入正确的手机号");
                    }
                }
                if (!s.toString().matches("[0-9]*") && s.length() != 0) {
                    mEditPhone.setError("您的输入非法");
                    mEditPhone.getText().delete(s.length() - 1, s.length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        //注册对提交结果同一判断
        mRegisterButton.setOnClickListener(v -> {
            String account = mEditAccount.getText().toString();
            String pwd = mEditPwd.getText().toString();
            String pwdCheck = mEditPwdCheck.getText().toString();
            String phone = mEditPhone.getText().toString();
            String email = mEditEmail.getText().toString();
            if (FormatUtils.isAccount(account) && FormatUtils.isPwd(pwd) && (pwd.equals(pwdCheck)) &&
                    FormatUtils.isPhone(phone) && FormatUtils.isEmail(email)) {
                SharedPreferences.Editor edit = getSharedPreferences(Constant.REGISTER, MODE_PRIVATE).edit();
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("account", account);
                hashMap.put("pwd", pwdCheck);
                hashMap.put("phone", phone);
                hashMap.put("email", email);
                edit.putString("value", new Gson().toJson(hashMap)).apply();

                ToastUtils.showToast(this, "注册成功", Toast.LENGTH_LONG);
                finish();
            } else {
                if (!FormatUtils.isAccount(account)) {
                    mEditEmail.setError("您的账号不符合规范");
                }
                if (!FormatUtils.isEmail(email)) {
                    mEditEmail.setError("您的邮箱不符合规范");
                }
                if (!FormatUtils.isPwd(pwd)) {
                    mEditEmail.setError("您的密码不符合规范");
                }
                if (!FormatUtils.isPhone(phone)) {
                    mEditEmail.setError("您的手机号不符合规范");
                }
            }
        });

    }


    private void initView() {

        initToolBar(this, R.string.register);

        mEditAccount = findViewById(R.id.edit_account);
        mEditEmail = findViewById(R.id.edit_email);
        mEditPwd = findViewById(R.id.edit_pwd);
        mEditPwdCheck = findViewById(R.id.edit_pwd_check);
        mEditPhone = findViewById(R.id.edit_phone);

        mRegisterButton = findViewById(R.id.registerButton);
    }
}
